myapp.models.Museum = Ext.regModel("myapp.models.Museum", {
    fields: [
        { name: 'id', type: 'int' },
        { name: 'museumName', type: 'string' },
        { name: 'address', type: 'string' },
        { name: 'telephone', type: 'string' },
        { name: 'hoursOfOperation', type: 'string' },
        { name: 'description', type: 'string' },
        { name: 'web', type: 'string' },
        { name: 'image', type: 'string' },
        { name: 'latitude', type: 'float' },
        { name: 'longitude', type: 'float' }
    ]
/*    fields: [
        { name: "id", type: "int" },
        { name: "givenName", type: "string" },
        { name: "familyName", type: "string" },
        { name: "emails", type: "auto" },
        { name: "phoneNumbers", type: "auto" },
    ]*/
});
myapp.stores.museums = new Ext.data.Store({
    model: "myapp.models.Museum",
    sorters: 'museumName',
    getGroupString: function(record){
    	return record.get('museumName')[0];
    },
    //    data: [
    //        { id: 1, givenName: 'Ed', familyName: 'Spencer' },
    //        { id: 2, givenName: 'Tommy', familyName: 'Maintz' },
    //        { id: 3, givenName: 'Aaron', familyName: 'Conran' },
    //        { id: 1, givenName: 'Ed', familyName: 'Spencer' },
    //        { id: 2, givenName: 'Tommy', familyName: 'Maintz' },
    //        { id: 3, givenName: 'Aaron', familyName: 'Conran' },
    //        { id: 1, givenName: 'Ed', familyName: 'Spencer' },
    //        { id: 2, givenName: 'Tommy', familyName: 'Maintz' },
    //        { id: 3, givenName: 'Aaron', familyName: 'Conran' },
    //        { id: 1, givenName: 'Ed', familyName: 'Spencer' },
    //        { id: 2, givenName: 'Tommy', familyName: 'Maintz' },
    //        { id: 3, givenName: 'Aaron', familyName: 'Conran' },
    //        { id: 1, givenName: 'Ed', familyName: 'Spencer' },
    //        { id: 2, givenName: 'Tommy', familyName: 'Maintz' },
    //        { id: 3, givenName: 'Aaron', familyName: 'Conran' },
    //        { id: 4, givenName: 'Jamie', familyName: 'Avins' }
    //    ]
    //
    proxy: {
        type: 'ajax',
        url: 'museumsJSON.js',
        reader: {
            type: 'json',
            root: 'museums'
        }
    },
    autoLoad: true,
    markersWithinBounds: function(bounds){
    	// bounds	.northeast	.lat
    	//						.lng
    	//			.southwest	.lat
    	//						.lng
    	var theReturn = [];
    	//var currentLat = myapp.geoLocation.latitude;
    	//var currentLon = myapp.geoLocation.longitude;
    	Ext.each(myapp.stores.museums.data.items, function (record){
    		var museum = record.data;
    		if (
    				museum.latitude <= bounds.northeast.lat
    				&& museum.latitude >= bounds.southwest.lat
    				&& museum.longitude <= bounds.northeast.lng
    				&& museum.longitude >= bounds.southwest.lng
    				)
    			theReturn.push(record.data);
    	});
    	//alert(theReturn.length);
    	return theReturn;
    },
    museumsWithinRange: function(currentLat, currentLon, rangeM){
    	var theReturn = [];
    	Ext.each(myapp.stores.museums.data.items, function (record){
    		var museum = record.data;
    		var distanceFrom = this.store.haversineDistance(museum.latitude, museum.longitude, currentLat, currentLon);
    		console.log('distance: ' + distanceFrom);
    		if (distanceFrom < rangeM) // radius in metres
    			theReturn.push(record.data);
    	});
    	return theReturn;
    },
    haversineDistance: function(lat1, lon1, lat2, lon2){
    	var R = 6371; // km
    	var d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
    	                  Math.cos(lat1)*Math.cos(lat2) *
    	                  Math.cos(lon2-lon1)) * R;    	
    	return d;
    }
});

